files.bash

#!/usr/bin/env bash

is_project_dir(){
    dir=$(project_dir)
    if [[ $dir != "" ]];then
        return 0;
    fi
    echo ""
    msg_mistake "$(pwd) is not a project directory. Try again. "
    echo ""
    return 1;
}

project_dir(){
    local dir;
    dir=$(git rev-parse --show-toplevel 2>/dev/null)
    echo $dir;
}

# Example:
# is_outside_project && msg_mistake "You're not currently in a project." && msg_instruct "Use [bent switch project]" && return;
is_outside_project(){
    dir=$(project_dir)
    if [[ $dir != "" ]];then
        return 1;
    fi
    return 0;
}



changed_files(){
    # @TODO use porcelain command?
    local files;
    files="$(git status -su)"
    msg "$files";
}
changed_files_array(){
    local -n files=${1}
    modType=${2:-"all"}

## Sample output from status
#  M .vscode/settings.json
#  D LICENSE
# AM cats
# D  code/help.sh
# A  f/1
# ?? blah.3
# ?? code/help.sh
# ?? dont-touch-cats-lol
# ?? f/2
    status="$(git status -su --porcelain=v1)"
    str_split_line "$status" files

}

conflicting_files(){
    local files;
    files="$(git diff --name-only --diff-filter=U)"
    msg "$files"
}

merge_conflicts(){
    # Any files with merge conflicts have <<<, ===, & >>>
    # <<<<<<<   =======  >>>>>>>i
    msg
    msg

    str_split_line "$(grep -r "^<<<<<<< " "$(git rev-parse --show-toplevel)")" files
    dir="$(project_dir)"
    bn="$(basename "${dir}")"
    for i in "${files[@]}"; do 
        file=$(sed -e "s/:<<<<<<< .*//" <<< "${i}")
        fileShort="${file#"${dir}"}"
        fileShort="${bn}${fileShort}"

        c1=$(grep -c "^=======" "${file}")
        c2=$(grep -c "^>>>>>>> " "${file}")
        if [[ c1 -gt 0 && c2 -gt 0 ]]; then
            echo "${fileShort} has a merge conflict"
        fi
    done
}